import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _isFunction from "lodash/isFunction";
var _excluded = ["componentCanDrag", "dropComponent"];

/**
 * 创建 可 拖动的 字段
 * @date 2019/1/4
 */
import React from 'react';
import { DragSource } from 'react-dnd'; //

import { DragType } from "../../config";
import createDropComponent from "../Drop/createDropComponent";
import styles from "../index.less?modules";
export default function createDrawDragComponent() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref$componentCanDrag = _ref.componentCanDrag,
      componentCanDrag = _ref$componentCanDrag === void 0 ? false : _ref$componentCanDrag,
      _ref$dropComponent = _ref.dropComponent,
      dropComponent = _ref$dropComponent === void 0 ? createDropComponent() : _ref$dropComponent,
      dragProps = _objectWithoutProperties(_ref, _excluded);

  var DropComponent = dropComponent;

  var DrawDragComponent = /*#__PURE__*/function (_React$Component) {
    _inherits(DrawDragComponent, _React$Component);

    var _super = _createSuper(DrawDragComponent);

    function DrawDragComponent(props) {
      var _this;

      _classCallCheck(this, DrawDragComponent);

      _this = _super.call(this, props);
      _this.state = {};
      _this.handleRemoveComponent = _this.handleRemoveComponent.bind(_assertThisInitialized(_this));
      _this.handleDrawComponentClick = _this.handleDrawComponentClick.bind(_assertThisInitialized(_this));
      return _this;
    }

    _createClass(DrawDragComponent, [{
      key: "handleDrawComponentClick",
      value: function handleDrawComponentClick(e) {
        // 阻止冒泡
        e.stopPropagation();
        var _this$props = this.props,
            onActiveComponent = _this$props.onActiveComponent,
            component = _this$props.component;

        if (_isFunction(onActiveComponent)) {
          onActiveComponent(component);
        }
      }
    }, {
      key: "render",
      value: function render() {
        var _this$props2 = this.props,
            _this$props2$componen = _this$props2.component,
            component = _this$props2$componen === void 0 ? {} : _this$props2$componen,
            onAddField = _this$props2.onAddField,
            onActiveField = _this$props2.onActiveField,
            onSwapField = _this$props2.onSwapField,
            onRemoveField = _this$props2.onRemoveField,
            children = _this$props2.children,
            currentEditComponent = _this$props2.currentEditComponent,
            currentEditField = _this$props2.currentEditField;
        return /*#__PURE__*/React.createElement("div", {
          className: styles['draw-drag-component'],
          onClick: this.handleDrawComponentClick
        }, /*#__PURE__*/React.createElement("span", {
          className: styles['draw-drag-component-remove'],
          onClick: this.handleRemoveComponent
        }), /*#__PURE__*/React.createElement(DropComponent, {
          component: component,
          onAddField: onAddField,
          onActiveField: onActiveField,
          onSwapField: onSwapField,
          onRemoveField: onRemoveField,
          currentEditComponent: currentEditComponent,
          currentEditField: currentEditField
        }, children));
      }
    }, {
      key: "handleRemoveComponent",
      value: function handleRemoveComponent(e) {
        // 阻止冒泡
        e.stopPropagation();
        var _this$props3 = this.props,
            component = _this$props3.component,
            onRemoveComponent = _this$props3.onRemoveComponent;

        if (_isFunction(onRemoveComponent)) {
          onRemoveComponent(component);
        }
      }
    }]);

    return DrawDragComponent;
  }(React.Component);

  if (componentCanDrag) {
    var _dragProps$drawType = dragProps.drawType,
        drawType = _dragProps$drawType === void 0 ? DragType.drawDragComponent : _dragProps$drawType,
        _dragProps$beginDrag = dragProps.beginDrag,
        beginDrag = _dragProps$beginDrag === void 0 ? defaultBeginDrag : _dragProps$beginDrag,
        _dragProps$canDrag = dragProps.canDrag,
        canDrag = _dragProps$canDrag === void 0 ? defaultCanDrop : _dragProps$canDrag;
    var drawDragComponentSpec = {
      beginDrag: beginDrag,
      canDrag: canDrag
    };

    var drawDragComponentCollect = function drawDragComponentCollect(connect, monitor) {
      var connectDragSource = connect.dragSource();
      var connectDragPreview = connect.dragPreview();
      var isDragging = monitor.isDragging();
      return {
        connectDragSource: connectDragSource,
        connectDragPreview: connectDragPreview,
        isDragging: isDragging
      };
    };

    return DragSource(drawType, drawDragComponentSpec, drawDragComponentCollect)(DrawDragComponent);
  }

  return DrawDragComponent;
}

function defaultBeginDrag(props) {
  return {
    component: props.component
  };
}

function defaultCanDrop() {
  return false;
}